﻿using System;
using System.IO;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.PhotoAlbum.Components;

namespace ASPNETVN.PORTAL.Modules.PhotoAlbum.Controls
{
    public partial class OrganizePicture : AddEditItemControl
    {
        protected string GetFlashVars()
        {
            return "?" + this.Server.UrlEncode(this.Request.QueryString.ToString());
        }

        protected static string GetFlashPath()
        {
            return Global.ApplicationPath + "/Modules/PhotoAlbum/FlashFileUpload.swf";
        }

        protected string GetFilePath(string filename)
        {
            string uploadPath = this.Settings["UploadPath"].ToString();
            return string.Format("~/" + uploadPath + "/PhotoAlbum/{0}/{1}", this.ModuleID, filename);
        }

        protected string GetThumbnailFilePath(string filename)
        {
            string uploadPath = this.Settings["UploadPath"].ToString();
            return string.Format("~/" + uploadPath + "/PhotoAlbum/{0}/Thumbnails/{1}", this.ModuleID, filename);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string jscript = "function UploadComplete(){";
            jscript += string.Format("__doPostBack('{0}','');", this.LinkButton1.ClientID.Replace("_", "$"));
            jscript += "};";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "FileCompleteUpload", jscript, true);
        }

        protected void dlPicture_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            string uploadPath = this.Settings["UploadPath"].ToString();

            int ret = Picture.Delete(Convert.ToInt32(this.dlPicture.DataKeys[e.Item.ItemIndex]));
            if (ret > 0)
            {
                // Xóa image.
                string filename = this.Server.MapPath("~/" + uploadPath + "/PhotoAlbum/" + this.ModuleID + "/" + e.CommandArgument);
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }

                // Xóa thumbnail image.
                string filenameThumbnail = this.Server.MapPath("~/" + uploadPath + "/PhotoAlbum/" + this.ModuleID + "/Thumbnails/" + e.CommandArgument);
                if (File.Exists(filenameThumbnail))
                {
                    File.Delete(filenameThumbnail);
                }


                this.dlPicture.DataBind();
            }
        }

        protected void dlPicture_EditCommand(object source, DataListCommandEventArgs e)
        {
            this.dlPicture.EditItemIndex = e.Item.ItemIndex;
            this.dlPicture.DataBind();
        }

        protected void dlPicture_CancelCommand(object source, DataListCommandEventArgs e)
        {
            this.dlPicture.EditItemIndex = -1;
            this.dlPicture.DataBind();
        }

        protected void dlPicture_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            TextBox txt = (TextBox)e.Item.FindControl("txtPictureTitle");
            Picture.UpdateTitle(Convert.ToInt32(this.dlPicture.DataKeys[e.Item.ItemIndex].ToString()), txt.Text);

            this.dlPicture.EditItemIndex = -1;
            this.dlPicture.DataBind();
        }

        protected void dlPicture_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "SetCover")
            {
                Picture.SetCover(Convert.ToInt32(this.dlPicture.DataKeys[e.Item.ItemIndex].ToString()));
                this.dlPicture.DataBind();
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            dlPicture.DataBind();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string url = string.Format("~/AddEditPage.aspx?tabindex={0}&tabid={1}&itemid={2}&mid={3}&page=AddEditCollection", this.TabIndex, this.TabID, this.ItemID, this.ModuleID);
            Response.Redirect(url);
        }
    }
}